class Mind::FriendsController < Mind::MindController
  upload_status_for :upload
  
  def upload_status
   render :inline => "<%= upload_progress.completed_percent rescue 0 %> % ", :layout => false
  end

#  def upload
#    @message = 'Error'
#    if request.post?
#      allowed = app_config[:icon_file_size].to_i.kilobytes
#      size = params[:friend][:image].size
#      if size < allowed
#        @message = h.number_to_human_size(size)
#        @friend = Friend.find(params[:friend][:id])
#        @friend.update_attributes(params[:friend])
#      else 
#        @message = _("overflow", h.number_to_human_size(size), h.number_to_human_size(allowed))  
#      end
#    end
#    finish_upload_status "'#{@message}'"
#  end


  def upload
    
    @message = 'Error'
    if request.post?
      allowed = app_config[:icon_file_size].to_i.kilobytes
      size = params[:face][:uploaded_data].size
      if size <= allowed
        @message = h.number_to_human_size(size)
        @friend = Friend.find(params[:friend][:id])
        face = Face.new(params[:face])
        if face.save
          @friend.face.destroy if @friend.face
          @friend.face = face
        else
         @message = _"No can do..."
        end
      else
        @message = _("overflow", h.number_to_human_size(size), h.number_to_human_size(allowed))
      end
    end
    finish_upload_status "'#{@message}'"
  end

  def icon_for_friend
    render :text => (h.friend_icon Friend.find(params[:id]))
  end

  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @pagination_opts = { :prev_label   => '&laquo; ',
                           :next_label   => ' &raquo;',
                           :inner_window => 3, 
                           :outer_window => 1 }
    @friends = Friend.mindlist params[:page]
    @friendships = Friend.find(current_friend.id).friendships
  end

  def show
    @friend = Friend.find(params[:id])
  end


  def edit
    @friend = Friend.find(params[:id])
  end

  def update
    @friend = Friend.find(params[:id])
    if @friend.update_attributes(params[:friend])
      redirect_to :action => 'show', :id => @friend
    else
      render :action => 'edit'
    end
  end

  def destroy
    Friendship.find(:all, :conditions => "friend_id = #{params[:id]} or dneirf_id = #{params[:id]}").each {|f| f.destroy}
    Friend.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
  
  
  #prijatelj o sebi
  def selfinfo
    if request.post?
      begin
        current_friend.update_attributes(params[:friend])
        current_user.save!
        flash[:notice] = 'Ok'
      rescue ActiveRecord::RecordInvalid => e
        flash[:error] = "Not OK #{e}" 
      end
    end
  end

  #family manager
  def family
    if request.xhr?
      @friendships = current_friend.friendships
      render :update do |page|
        page.replace_html 'familyul', :partial => 'common/friendship', :collection => @friendships
      end
    end
  end
  
  #add a friend to current_friend's family
  def familyadd
    if request.xhr?   
      dneirf = params[:id].split('friendrop')     
      if current_friend.has_familyfriend? dneirf.last
          render :update do |page|
          end
      else
        @friendship = Friendship.new
        @friendship.friend = current_friend
        @friendship.add_hint :show
        @familyfriend = Friend.find(dneirf.last)
        @friendship.dneirf = @familyfriend
        if @friendship.save       
          render :update do |page|
            page.insert_html :bottom, 'familyul', :partial => 'common/friendship', :locals => {:friendship => @friendship}
          end
        end
      end
    end    
  end
  
  #remove a friend from current_friend's family
  def familydel
    if request.xhr?
      friendship = Friendship.find(:first, :conditions => {:friend_id => current_friend.id, :dneirf_id => params[:id]})
      if friendship
        friendship.destroy
      end
      @friendships = current_friend.friendships
      render :update do |page|
        page.replace_html 'familyul', :partial => 'common/friendship', :collection => @friendships
      end
    end
  end
  
  #sporočilo
  def selfmessage
  end
  
  private
  
  def getfamily
    @friendships = Friend.find(params[:id]).friendships
  end

end